package com.chiyoyo.rabbitmq.framework.service.impl;

import com.chiyoyo.rabbitmq.framework.annotation.RabbitMq;
import com.chiyoyo.rabbitmq.framework.service.AbstractMQService;
import org.springframework.amqp.core.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chil
 * RabbitMq延迟队列注册
 */
@Service("delayedMQService")
public class DelayedMQService extends AbstractMQService {

    @Override
    protected AbstractExchange initExchange(String exchangeName, RabbitMq rabbitMq) {
        Map<String, Object> props = new HashMap<>(1);
        //  设置延迟交换器的类型，该类型由RabbitMq配置类提供
        props.put("x-delayed-type", rabbitMq.exchangeType());
        //  创建一个自定义交换器，使用延迟消息类型
        return new CustomExchange(exchangeName, "x-delayed-message", true, false, props);
    }

    @Override
    protected Binding initBinding(Queue queue, AbstractExchange exchange, String routingKey, RabbitMq rabbitMq) {
        //  绑定队列到交换器，使用指定的路由键，无额外参数
        return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
    }
}
